

Dim doc
Set doc = application.ActiveDocument
GetUserLicence(doc)
main
sub main()

	Dim docMenuBar
	Set docMenuBar = Gui.CommandBars("Document Menu Bar") 
	Set filesys = CreateObject("Scripting.FileSystemObject")
	file = "E:\output.txt"
	Set filetxt = filesys.CreateTextFile(file, True)
	
	xTab = vbTab

	For i = 1 To docMenuBar.Controls.Count
		 Set menu = docMenuBar.Controls.Item(i)
		 filetxt.WriteLine "+" & menu.Caption
		 Call WriteMenuIDs(menu)
		 filetxt.WriteLine
	Next
	filetxt.close
	set filetxt=nothing
	set filesys=nothing
	
	dim wsh
	set wsh = createobject("wscript.shell")
	wsh.run file
	set wsh = nothing
end sub


Sub WriteMenuIDs(menu)
     Set menuCtrls = menu.Controls
     For j = 1 To menuCtrls.Count
         cmdName = menuCtrls.Item(j).Caption
         On Error Resume Next
         id = menuCtrls.Item(j).Id
         If Err Then
             ' CommandBarPopup doesn't support Id property
             Err.Clear
             filetxt.WriteLine xTab & cmdName
             saveTab = xTab
             xTab = xTab & vbTab
             Call WriteMenuIDs(menuCtrls.Item(j))
             xTab = saveTab
         ElseIf id <> 0 Then
             ' Don't write out separator whose command id is 0
             filetxt.WriteLine xTab & cmdName & " : " & id
         End If
     Next
End Sub

 



'#################################################################
'#################################################################
'#################################################################
'#################################################################
' Function to validate document
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function

sub GetUserLicence(docObj)

	If ValidateServer(docObj) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If
	
end sub